using System;
using System.Collections.Generic;
using System.Linq;
using Dapper;
using Read.Models;
using Read.Utility;

namespace Read.Services
{
    public class RoleService : BaseService
    {
        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        public List<RoleModel> getRoleList(QueryParamsRole query)
        {
            var rolelist = new List<RoleModel>();
            string sql = $"SELECT * FROM `{Tables.Role}` WHERE Status = 1 AND Name LIKE @Name ORDER BY `Sort`";
            rolelist = connection.Query<RoleModel>(sql, new { Name = "%" + query.Name + "%" }).ToList();
            return rolelist;
        }

        /// <summary>
        /// 角色授权
        /// </summary>
        /// <returns></returns>
        public bool authorization(int roleId, string nodes)
        {
            //删除之前的权限，后再添加
            string sqlDel = $"DELETE FROM `{Tables.Access}` WHERE RoleId = @RoleId";
            string sqlStr = $"INSERT INTO `{Tables.Access}`(RoleId,NodeId,Level,Pid) VALUES (@RoleId,@NodeId,@Level,@Pid)";
            connection.Execute(sqlDel, new { RoleId = roleId });
            var ns = nodes.Split(',').ToList();
            var param = new List<AccessModel>();
            //Console.WriteLine("============" + ns.ToJson());
            foreach (var item in ns)
            {
                if (!string.IsNullOrEmpty(item))
                {
                    //Console.WriteLine("--------------" + item);
                    var access = new AccessModel();
                    access.RoleId = roleId;
                    access.NodeId = item.ToInt32();
                    access.Level = 0;
                    access.Pid = 0;
                    param.Add(access);
                }
            }
            try
            {
                connection.Execute(sqlStr, param);
            }
            catch (Exception err)
            {
                return false; throw err;
            }
            return true;
        }
        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int roleAdd(RoleModel model)
        {
            if (string.IsNullOrEmpty(model.Name))
            {
                return 1;
            }
            model.Pid = 1;
            model.Status = 1;
            model.CreateTime = Tools.ToUnixTime();
            model.UpdateTime = Tools.ToUnixTime();
            return db.Insert(model);
        }

        /// <summary>
        /// 根据Id查询角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public RoleModel getRoleById(int id)
        {
            var sql = $"SELECT * FROM  `{Tables.Role}` WHERE Id=@Id";
            return connection.QueryFirst<RoleModel>(sql, new { id = id });
        }

        /// <summary>
        /// 角色编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool roleEdit(RoleModel model)
        {
            RoleModel role = getRoleById(model.Id);
            //Console.WriteLine("++++++++++++++++++" + role.ToJson());
            model.Pid = role.Pid;
            model.Status = role.Status;
            model.CreateTime = role.CreateTime;
            model.UpdateTime = Tools.ToUnixTime();
            return db.Update(model);
        }

        /// <summary>
        /// 角色删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool roleDel(int id)
        {
            RoleModel role = getRoleById(id);
            //Console.WriteLine(admin.ToJson());
            //return true;
            return db.Delete(role);
        }
    }
}